import { APP_INITIALIZER, Injectable, Provider, inject } from '@angular/core';
import { ACLService } from '@delon/acl';
import { ALAIN_I18N_TOKEN, MenuService, SettingsService, TitleService } from '@delon/theme';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
import { Observable, map } from 'rxjs';

import { I18NService } from '../i18n/i18n.service';

export function provideStartup(): Provider[] {
  return [
    StartupService,
    {
      provide: APP_INITIALIZER,
      useFactory: (startupService: StartupService) => () => startupService.load(),
      deps: [StartupService],
      multi: true
    }
  ];
}

@Injectable()
export class StartupService {
  private menuService = inject(MenuService);
  private settingService = inject(SettingsService);
  private aclService = inject(ACLService);
  private titleService = inject(TitleService);
  private i18n = inject<I18NService>(ALAIN_I18N_TOKEN);

  private viaHttp(): Observable<void> {
    const defaultLang = this.i18n.defaultLang;
    return this.i18n.loadLangData(defaultLang).pipe(
      map((langData: NzSafeAny) => {
        this.i18n.use(defaultLang, langData);
        this.viaMock();
      })
    );
  }
  private viaMock(): void {
    const app: any = {
      name: `风灯网盘`,
      description: `中小企业自己的网盘`
    };
    this.settingService.setApp(app);
    this.aclService.setFull(true);
    this.menuService.add([
      {
        text: '我的文件',
        group: true,
        children: [
          {
            text: '全部文件',
            link: '/dashboard',
            icon: { type: 'icon', value: 'copy', theme: 'twotone' }
          },
          {
            text: '图片',
            link: '/image',
            icon: { type: 'icon', value: 'file-image', theme: 'twotone' }
          },
          {
            text: '视频',
            link: '/video',
            icon: { type: 'icon', value: 'video-camera', theme: 'twotone' }
          },
          {
            text: '音频',
            link: '/sound',
            icon: { type: 'icon', value: 'sound', theme: 'twotone' }
          },
          {
            text: '文档',
            link: '/file',
            icon: { type: 'icon', value: 'file-pdf', theme: 'twotone' }
          },
          {
            text: '压缩文件',
            link: '/zip',
            icon: { type: 'icon', value: 'file-zip', theme: 'twotone' }
          },
          {
            text: '其他',
            link: '/unknown',
            icon: { type: 'icon', value: 'file-unknown', theme: 'twotone' }
          }
        ]
      },
      {
        text: '组文件',
        group: true,
        children: [
          {
            text: '共享柜',
            link: '/share',
            icon: { type: 'icon', value: 'hdd', theme: 'twotone' },
            shortcutRoot: true
          },
          {
            text: '组文件',
            link: '/team',
            icon: { type: 'icon', value: 'hdd', theme: 'twotone' },
            shortcutRoot: true
          }
        ]
      },
      {
        text: '系统管理',
        group: true,
        children: [
          {
            text: '用户与组',
            link: '/user',
            icon: { type: 'icon', value: 'contacts', theme: 'twotone' },
            shortcutRoot: true
          }
        ]
      }
    ]);
    this.titleService.suffix = app.name;
  }

  load(): Observable<void> {
    return this.viaHttp();
  }
}
